unit uClassePesquisaAux;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, BufDataset, db, dialogs, sqldb , DbCtrls,typinfo;

type

  { TPesquisaForm }

  TPesquisaForm = class
    private
      FTabelaPesquisa: string;
      procedure SetTabelaPesquisa(AValue: string);

    public
      property TabelaPesquisa : string read FTabelaPesquisa write SetTabelaPesquisa;
  end;

  { TCamposPesquisa }

  TCamposPesquisa = class
    private
      var BufDataSetCampos : TBufDataset;
      var vDS : TDataSource;
    public
      constructor Create;
      destructor Destroy;override;
      procedure CarregarCampos (qry : TSQLQuery;lookupComboBox : TDBLookupComboBox);overload;
      procedure CarregarCampos; overload;
      procedure TesteBuf;
  end;

  TCampoPesquisa = class

  end;

implementation

{ TCamposPesquisa }

constructor TCamposPesquisa.Create;
begin
  Self.BufDataSetCampos := TBufDataset.Create(nil);
  Self.vDS := TDataSource.Create(nil);
  with Self.BufDataSetCampos do
  begin
    FieldDefs.add('id',ftAutoInc);
    FieldDefs.Add('nomecampo',ftString,50); // --> apelido tabela mais nome campo ex: t1.nome
    FieldDefs.Add('labelcampo',ftString,50);
    FieldDefs.Add('tipocampo',ftString,50);
    FieldDefs.Add('tabela',ftstring,50);
    FieldDefs.Add('exibenodbgrid',ftBoolean);
    FieldDefs.Add('tamanhocampo',ftInteger);
    FieldDefs.Add('tamanhoexibicao',ftInteger);
    FieldDefs.Add('usarelacionamento',ftBoolean);
    FieldDefs.Add('tabela2',ftString,50);
    FieldDefs.Add('tiporelacionamento',ftString);
    FieldDefs.Add('campo2',ftString);
    FieldDefs.Add('campochavetabela1',ftString);
    FieldDefs.Add('campochavetabela2',ftString);
    FieldDefs.Add('apelidocampo',ftString);
    FieldDefs.Add('apelidotabela',ftString);
    AddIndex('id','id',[]);
    IndexName:= 'id';
    CreateDataset;
  end;
  //tin
end;

destructor TCamposPesquisa.Destroy;
begin
  Self.BufDataSetCampos.Free;
  inherited Destroy;
end;

procedure TCamposPesquisa.CarregarCampos(qry: TSQLQuery;
  lookupComboBox: TDBLookupComboBox);
var i : integer;
begin
  for i := 0 to qry.FieldDefs.Count - 1 do
  begin
    BufDataSetCampos.CreateDataset;
    with BufDataSetCampos do
    begin
      Insert;
      FieldByName('nomecampo').AsString:= qry.FieldDefs[i].Name;
      FieldByName('labelcampo').AsString:= qry.FieldDefs[i].DisplayName;
      FieldByName('tipocampo').AsString:= GetEnumName(TypeInfo(TFieldType),Integer(qry.FieldDefs[i].DataType));

    end;
  end;
  {for i := 0 to 5 do
  begin
    with BufDataSetCampos do
    begin
      Insert;
      FieldByName('nomecampo').AsString:= 'campo' + IntToStr(i);
      Post;
    end;
  end;

  vDS.DataSet := BufDataSetCampos;
  //lookupComboBox.DataSource := vDS;
  lookupComboBox.ListSource := vDS;
  lookupComboBox.ListField:= 'nomecampo';
  lookupComboBox.KeyField:= 'id';}

end;

procedure TCamposPesquisa.CarregarCampos;
var qry : TSQLQuery = nil;
begin


end;

procedure TCamposPesquisa.TesteBuf;
var i : integer;
begin

  for i := 0 to 5 do
  begin
    //ShowMessage(IntToStr(i));
    with Self.BufDataSetCampos do
    begin
      Insert;
      FieldByName('nomecampo').AsString:= 'nome' + IntToStr(i);
      Post;
    end;
  end;

  Self.BufDataSetCampos.First;

  while not Self.BufDataSetCampos.EOF do
  begin

    ShowMessage(Self.BufDataSetCampos.FieldByName('nomecampo').AsString);
    Self.BufDataSetCampos.Next;
  end;
end;

{ TPesquisaForm }

procedure TPesquisaForm.SetTabelaPesquisa(AValue: string);
begin
  if FTabelaPesquisa=AValue then Exit;
  FTabelaPesquisa:=AValue;
end;

end.

